package com.cisdi.analysis.web;

import com.cisdi.analysis.core.dto.AnalysisDataDTO;
import org.supercsv.cellprocessor.constraint.DMinMax;
import org.supercsv.cellprocessor.constraint.NotNull;
import org.supercsv.cellprocessor.ift.CellProcessor;
import org.supercsv.io.CsvBeanReader;
import org.supercsv.io.ICsvBeanReader;
import org.supercsv.prefs.CsvPreference;

import java.io.FileReader;

/**
 * @author liujunnan
 * @date 2018/5/23
 */
public class ReadingObjects {
    public static void main(String[] args) throws Exception {
        //map格式
       /* ICsvMapReader mapReader = null;
        try {
            mapReader = new CsvMapReader(new FileReader("E:\\123.csv"), CsvPreference.STANDARD_PREFERENCE);
            // the header columns are used as the keys to the Map
            mapReader.getHeader(true);
            final String[] header = {"one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve"};
            final CellProcessor[] processors = getProcessors();
            Map<String, Object> customerMap;
            while ((customerMap = mapReader.read(header, processors)) != null) {
                System.out.println(String.format("lineNo=%s, rowNo=%s, customerMap=%s", mapReader.getLineNumber(),
                        mapReader.getRowNumber(), customerMap));
            }

        } finally {
            if (mapReader != null) {
                mapReader.close();
            }
        }*/
        //对象格式
        ICsvBeanReader beanReader = null;
        try {
            beanReader = new CsvBeanReader(new FileReader("E:\\123.csv"), CsvPreference.STANDARD_PREFERENCE);
            // the header columns are used as the keys to the Map
            beanReader.getHeader(true);
            final String[] header = {"one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve", "quality"};
            final CellProcessor[] processors = getProcessors();
            AnalysisDataDTO analysisDataDTO;
            while ((analysisDataDTO = beanReader.read(AnalysisDataDTO.class, header, processors)) != null) {
                System.out.println(analysisDataDTO.getOne());
            }
        } finally {
            if (beanReader != null) {
                beanReader.close();
            }
        }
    }

    private static CellProcessor[] getProcessors() {

        final CellProcessor[] processors = new CellProcessor[]{
                new DMinMax(-DMinMax.MAX_DOUBLE, DMinMax.MAX_DOUBLE),
                new DMinMax(-DMinMax.MAX_DOUBLE, DMinMax.MAX_DOUBLE),
                new DMinMax(-DMinMax.MAX_DOUBLE, DMinMax.MAX_DOUBLE),
                new DMinMax(-DMinMax.MAX_DOUBLE, DMinMax.MAX_DOUBLE),
                new DMinMax(-DMinMax.MAX_DOUBLE, DMinMax.MAX_DOUBLE),
                new DMinMax(-DMinMax.MAX_DOUBLE, DMinMax.MAX_DOUBLE), // married
                new DMinMax(-DMinMax.MAX_DOUBLE, DMinMax.MAX_DOUBLE),// numberOfKids
                new DMinMax(-DMinMax.MAX_DOUBLE, DMinMax.MAX_DOUBLE),
                new DMinMax(-DMinMax.MAX_DOUBLE, DMinMax.MAX_DOUBLE),
                new DMinMax(-DMinMax.MAX_DOUBLE, DMinMax.MAX_DOUBLE), // loyaltyPoints
                new DMinMax(-DMinMax.MAX_DOUBLE, DMinMax.MAX_DOUBLE),// loyaltyPoints
                new DMinMax(-DMinMax.MAX_DOUBLE, DMinMax.MAX_DOUBLE),// loyaltyPoints
                new NotNull() // loyaltyPoints
        };
        return processors;
    }
}
